/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
import model.Usuario;
import model.Viaje;
import model.SugerenciaViaje;
import java.util.List;
import org.springframework.beans.support.PagedListHolder;
import service.UsuarioService;
import service.ViajeService;
import funcion.enviarMail;
import java.util.ArrayList;
import org.springframework.web.servlet.mvc.Controller;
import org.apache.log4j.*;
import org.apache.log4j.xml.DOMConfigurator;
import service.SugerenciaService;

/**
 *@author Fatima Pestana
 * @author Geral Zerpa
 */
public class SugerenciasdelViaje implements Controller {

    private UsuarioService usuarioService;
    private ViajeService viajeService;
    private SugerenciaService sugerenciaService;
    private static Logger loggers = null;

    public void setViajeService(ViajeService viajeService) {
        this.viajeService = viajeService;
    }

    public void setSugerenciaService(SugerenciaService sugerenciaService) {
        this.sugerenciaService = sugerenciaService;
    }

    public void setUsuarioService(UsuarioService usuarioService) {
        this.usuarioService = usuarioService;
    }

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map model = new HashMap();

        PagedListHolder pagedListHolder1 = (PagedListHolder) request.getSession().getAttribute("clienteList");

        Usuario user2;
        Viaje via;
        System.out.println("aqui voy de nuevo");
        user2 = (Usuario) usuarioService.getUsuario(Long.parseLong(request.getParameter("ux")));
        via = (Viaje) viajeService.getViaje(Long.parseLong(request.getParameter("mv")));
        Long Valor;
        Valor = Long.parseLong(request.getParameter("AM"));

        if (Valor == 1) {
            pagedListHolder1 = new PagedListHolder(sugerenciaService.getListaSugerencia(Long.parseLong(request.getParameter("mv"))));

        } else {
            String page = (String) request.getParameter("page");
            if ("next".equals(page)) {
                pagedListHolder1.nextPage();
            } else if ("previous".equals(page)) {
                pagedListHolder1.previousPage();
            }
        }

        request.getSession().setAttribute("clienteList", pagedListHolder1);
        model.put("clienteList", pagedListHolder1);
        model.put("usuario", user2);
        model.put("viaje", via);
        return new ModelAndView("SugerenciasdelViaje", model);

    }
}


